
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: whh
 * @Date: 2021/6/25
 * @Description:
 */
public class project {
    public static String calculate(String arg){
        String result = "";
        String[] split = arg.split(";");
        String symbols = "*/+-";
        for (int i = 0; i < split.length; i++) {
            String expo = split[i];
            Map<Integer,BigDecimal> afterMultiplyAndDivideValueMap = new HashMap<>();
            ArrayList<String> addAndSubtractStringList = new ArrayList<>();
            int n = 0;
            for (int j = 0; j < expo.length(); j++) {
                if(afterMultiplyAndDivideValueMap.get(n) == null){
                    for (int k = j+1; k < expo.length(); k++) {
                        String ch = String.valueOf(expo.charAt(k));
                        if(symbols.contains(ch) || ch.equals("=")){
                            afterMultiplyAndDivideValueMap.put(n,new BigDecimal(expo.substring(j,k)));
                            break;
                        }
                    }
                    continue;
                }
                String firstChar = String.valueOf(expo.charAt(j));
                if(symbols.contains(firstChar)){
                    if(firstChar.equals("+") || firstChar.equals("-")){
                        n++;
                        addAndSubtractStringList.add(firstChar);
                        continue;
                    }
                    for (int k = j+1; k < expo.length(); k++) {
                        String secondChar = String.valueOf(expo.charAt(k));

                        if(symbols.contains(secondChar) || secondChar.equals("=")){
                            BigDecimal bigDecimal = new BigDecimal(expo.substring(j + 1, k));
                            if(null == afterMultiplyAndDivideValueMap.get(n)){
                                afterMultiplyAndDivideValueMap.put(n,bigDecimal);
                            }
                            if(firstChar.equals("*") || firstChar.equals("/")){
                                afterMultiplyAndDivideValueMap.put(n,cal(afterMultiplyAndDivideValueMap.get(n), firstChar, bigDecimal));
                            }
                            j=k-1;
                            break;
                        }
                    }
                }
            }
            BigDecimal value = afterMultiplyAndDivideValueMap.get(0);
            for (int j = 0; j < addAndSubtractStringList.size(); j++) {
                String s = addAndSubtractStringList.get(j);
                value = cal(value, s, afterMultiplyAndDivideValueMap.get(j + 1));
            }
            expo = expo+value.toString();
            result += expo+"\n";
        }
        return result;
    }

    public static BigDecimal cal(BigDecimal data1,String op,BigDecimal data2){
        switch (op){
            case "*":return data1.multiply(data2).setScale(2,BigDecimal.ROUND_HALF_UP);
            case "/":return data1.divide(data2,2,BigDecimal.ROUND_HALF_UP);
            case "+":return data1.add(data2).setScale(2,BigDecimal.ROUND_HALF_UP);
            case "-":return data1.subtract(data2).setScale(2,BigDecimal.ROUND_HALF_UP);
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(calculate("2*3/7-1*6+4=;1.5+1/4*3.243-5*6=;2.32+30/4-64*1=;"));
    }
}
